var glass = glass || (function() {
  function selectTab($that) {
    if($that == undefined || $that.length == 0)
    {
      console.log('Undefined tab');
      return;
    }
    var index = -1;
    $('.dialog .nav li').each(function(indexInArray){
      $this=$(this);
      $this.toggleClass('selected',false);
      if($this.is($that))
        index = indexInArray
    });
    $that.toggleClass('selected',true);
    $root = $($that.parent().parent());

    $panes = $root.children('div.controls');
    $panes.hide();
    $div = $($panes[index]);
    $div.show();

    var thatPosition = $that.position();
    var $navBorders = $root.children('div.nav-border');
    var $left = $($navBorders[0])
    $left.width(thatPosition.left+2); //Add in the border size
    $left.css('left','0px');

    var $right = $($navBorders[1])
    var rightSide = (thatPosition.left + $that.outerWidth())
    $right.css('left',(rightSide-2)+'px');
    $right.width($root.width()-rightSide+4);
  }

  $(document).ready(function(){
    $('body').on('click','.dialog .nav li',function(){ selectTab($(this)); } );
  });

  function reselect(){
    selectTab($('.dialog .nav li.selected')); 
  }

  function think($that) {
    $that.toggleClass('thinking',true);
  }

  function unthink($that) {
    $that.toggleClass('thinking',false);
  }

  return {
    selectTab: selectTab,
    reselect: reselect,
    think: think,
    unthink: unthink
  };
})();
